//
//  WKNavView.swift
//  NewDelivery
//
//  Created by 天下宅 on 2016/11/7.
//  Copyright © 2016年 天下宅. All rights reserved.
//

import UIKit


enum WKNavViewClickType:UInt {
    case left = 0,right = 1
}

protocol WKNavViewProtocol {
    func navBtnClick(WithType type:WKNavViewClickType)
}


class WKNavView: UIView {

    var _title:String
    {
        set{
            titleLabel.text = newValue
        }
        get{
            return titleLabel.text ?? ""
        }
    }
    var leftTitle:String = ""
    {
        didSet{
            leftBtn.setTitle(leftTitle, for: UIControlState.normal)
            leftBtn.layout(withContentModel:.left,contentInsideModel: .left, space: 5)

        }

    }
    var rightTitle:String = ""
    {
        didSet{
            rightBtn.setTitle(rightTitle, for: UIControlState.normal)
            rightBtn.layout(withContentModel:.right,contentInsideModel: .right, space: 5)
        }

    }
    
    var leftImage:UIImage?
    {
        didSet{
            leftBtn.setImage(leftImage, for: .normal)
            leftBtn.layout(withContentModel:.left,contentInsideModel: .left, space: 5)
        }
    }
    var rightImage:UIImage?
    {
        didSet{
            rightBtn.setImage(rightImage, for: .normal)
            rightBtn.layout(withContentModel:.right,contentInsideModel: .right, space: 5)
        }

    }

    var leftBtnEnable = true
    {
        didSet{
            leftBtn.isHidden = !leftBtnEnable
        }
    }
    var rightBtnEnable = false
    {
        didSet{
            rightBtn.isHidden = !rightBtnEnable
        }
    }
    
    
    
    private var titleLabel = UILabel()
    private var leftBtn:UIButton = UIButton.init(type: UIButtonType.custom)
    private var rightBtn:UIButton = UIButton.init(type: UIButtonType.custom)
    private var vc:WKNavViewProtocol?
    
    
    static func creatNavView(WithTitle title:String = "",vProtocol:WKNavViewProtocol) -> WKNavView
    {
        let nav = WKNavView()
        nav.setInterFace()
        nav._title = title
        nav.addLine(WithColor: UIColor.lightGray, model: [.bottom])
        nav.vc = vProtocol
        return nav
    }
    

    
    func setInterFace()  {
        self.frame = CGRect.init(x: 0, y: 0, width: screenWidth, height: 64)
        self.backgroundColor = UIColor.white
        titleLabel.textAlignment = NSTextAlignment.center
        titleLabel.font = UIFont.systemFont(ofSize: 22)
        titleLabel.textColor = UIColor.black
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(20)
            make.bottom.equalToSuperview()
            make.centerX.equalToSuperview()
            make.width.lessThanOrEqualTo(150)
        }
        
        leftBtn.frame = CGRect.init(x: 0, y: 20, width: 80, height: 44)
        self.addSubview(leftBtn)
        leftBtn.addTarget(self, action: #selector(btnClick(btn:)), for: .touchUpInside)
        leftBtn.tag = 100
        leftBtn.setTitleColor(UIColor.black, for: .normal)
        
        
        rightBtn.frame = CGRect.init(x: screenWidth - 80, y: 20, width: 80, height: 44)
        self.addSubview(rightBtn)
        rightBtn.addTarget(self, action: #selector(btnClick(btn:)), for: .touchUpInside)
        rightBtn.tag = 101
        rightBtn.setTitleColor(UIColor.black, for: .normal)

        rightBtn.isHidden = true

        
    }
    
    
    func btnClick(btn: UIButton)
    {
        let index = btn.tag - 100
        let type = index == 0 ? WKNavViewClickType.left : WKNavViewClickType.right
        vc?.navBtnClick(WithType: type)
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
